﻿using MicroDb;
using MicroDb.Helper;
using MicroDb.Querys;

using Microsoft.Extensions.Options;

#if NETCOREAPP
using Microsoft.AspNetCore.Builder;
#endif
using System;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class MicroDbServiceExtension
    {
        /// <summary>
        /// 启用数据库
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddMicroDb(this IServiceCollection services)
        {
            services.AddTransient(typeof(IQueryable<>), typeof(DBQueryable<>));
            services.AddSingleton<IConfigureOptions<MicroDbOptions>, MicroDbConfigure>();
            services.AddSingleton<CodeFirst>();
            return services;
        }
        /// <summary>
        /// 启用数据库
        /// </summary>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddMicroDb(this IServiceCollection services, Action<MicroDbOptions> action)
        {
            return services.AddMicroDb().Configure(action);
        }

#if NETCOREAPP

        public static IApplicationBuilder UseCodeFirst(this IApplicationBuilder app,Type[] types)
        {
            var codeFirst = app.ApplicationServices.GetService<CodeFirst>();
            codeFirst.Use(types);
            return app;
        }
#endif

    }
}
